﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using HPITRFID;
using MODEL;
using System.Runtime.InteropServices;
using System.IO.Ports;
using System.Threading;

namespace StudentManage
{
    public partial class Frm_Main : Form
    {
        //打开串口
        [DllImport("HPITRFID/EPCSDK.dll")]
        public static extern IntPtr OpenComm(int portNo);
        //关闭串口
        [DllImport("HPITRFID/EPCSDK.dll")]
        public static extern void CloseComm(IntPtr hCom);

        //定义串口对象
        public static IntPtr handle = IntPtr.Zero;

        //定义串口打开状态的属性
        public bool HandleStatus
        {
            get// 获取，读取
            {
                return (int)handle != -1 && handle != IntPtr.Zero ? true : false;
            }
        }

        

        DataGridView dgv1;
        public Frm_Main()
        {
            InitializeComponent();
        }
        public Frm_Main(DataGridView dgv1)
        {
            InitializeComponent();
            this.dgv1 = dgv1;
        }
       

        ListBox listBox;
        public Frm_Main(ListBox listBox)
        {
            this.listBox = listBox;
            InitializeComponent();
        }
        

        //Frm_Login login;
        //public Frm_Main(Frm_Login frm)
        //{
        //    login = frm;
        //    InitializeComponent();
        //}


        private void Frm_Main_Load(object sender, EventArgs e)
        {
            //Frm_List frm_List = new Frm_List();
            ////打开日志窗体
            //frm_List.MdiParent = this;
            //frm_List.Show();

            //打开串口
            try
            {
                if (HandleStatus != true)
                {
                    handle = OpenComm(4);
                }
                //启动计时器
                timer1.Start();
                
            }
            catch
            {
                MessageBox.Show("串口未成功打开,请在设备参数中打开串口，并启动打卡", "系统提示");
            }
            try
            {
                byte[] data = new byte[1];

                //设置工作模式
                data[0] = 2;
                EPCSDKHelper.SetReaderParameters(handle, 0x70, 1, data, 0);

                //设置读卡的读卡方式
                data[0] = 1;
                EPCSDKHelper.SetReaderParameters(handle, 0x87, 1, data, 0);

                //设置相邻判别参数
                data[0] = 2;
                EPCSDKHelper.SetReaderParameters(handle, 0x7B, 1, data, 0);

                //设置相邻判别时间
                data[0] = 0;
                EPCSDKHelper.SetReaderParameters(handle, 0x7A, 1, data, 0);
                
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void 学生管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_Select _Select = new Frm_Select();
            _Select.MdiParent = this;

            _Select.Show();
        }

        private void 考勤ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_Work frm_Work = new Frm_Work();
            frm_Work.MdiParent = this;
            frm_Work.Show();
        }

        private void 考勤规则ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_Parameter frm_Parameter = new Frm_Parameter();
            frm_Parameter.MdiParent = this;
            frm_Parameter.Show();
        }


    private void 退出登录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定退出登录吗？","提示信息",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
            //定义打开窗体的方法
            void RunFrm()
            {
                new Frm_Login().ShowDialog();
            }
            if (dr==DialogResult.Yes)
            {
                //打开登录窗体，退出此窗体
                Thread th = new Thread(RunFrm);
                th.Start();
                this.Close();
            }
        }

        private void 设备参数ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_CanShu frm_CanShu = new Frm_CanShu();
            frm_CanShu.MdiParent = this;
            frm_CanShu.Show();

        }

        private void 考勤管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_Class frm_Class = new Frm_Class();
            frm_Class.MdiParent = this;
            frm_Class.Show();
        }

        private void 层叠排列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void 水平平铺ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void 垂直平铺ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void 整理窗口ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void 账户信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Frm_Account frm_Account = new Frm_Account();
            //frm_Account.MdiParent = this;
            //frm_Account.Show();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //Thread.Sleep(100);
            try
            {
                //读取多标签数量
                byte tagNum = 0;
                //接收标签Id的数组
                byte[] tagsID = new byte[12 * 200];

                //读取多标签
                if (EPCSDKHelper.IdentifyUploadedMultiTags(handle, out tagNum, tagsID, null, null))
                {
                    //说明读取成功
                    for (int i = 0; i < tagNum; i++)
                    {
                        byte[] data = new byte[12];

                        //截取数组中的每一个标签，赋值给data
                        Buffer.BlockCopy(tagsID, i * 12, data, 0, 12);
                        //this.listBox1.Items.Add();

                        
                        Work work = new Work();
                        Frm_Work frm_Work = new Frm_Work();
                       
                        
                        work.W_StuCardId = ConvertHelper.ByteArrayToHexString(data);
                        work.W_State = true;
                        work.W_BeiZhu = "";
                        //执行打卡的存储过程
                        int num = BLL.WorkManage.DaKa(work);
                        if (num>0)
                        {
                            //MessageBox.Show("读取成功");
                            //Frm_List.listBox1.Items.Add("");
                        }
                        //刷新表
                        //this.dgv1.DataSource = BLL.WorkManage.ChaXunClass();
                    }
                }
                else
                {
                    //读取失败
                    //MessageBox.Show("读取失败");
                    //Frm_List.listBox1.Items.Add("读取
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //Frm_List.listBox1.Items.Add(ex.Message);
            }
        }
    }
}
